<?php
namespace Wechat\Controller;

class WxMenuController extends CommonController {
    
    /**
     * 自定义菜单
     */
    public function index(){
        $list = M("WxMenu")->where("customer_id='{$this->customer_id}'")->order("id desc")->select();
        foreach($list as $v){
            if($v['position'] == 1){
                $first[] = $v;
            }elseif($v['position'] == 2){
                $second[] = $v;
            }elseif($v['position'] == 3){
                $third[] = $v;
            }
        }
        $this->assign("first",$first);
        $this->assign("second",$second);
        $this->assign("third",$third);
        $this->display();
    }

    /**
     * 保存菜单
     */
    public function saveMenu(){
        $data = $_REQUEST;
        $menu = M('WxMenu')->where(array("position"=>I("position"),'customer_id'=>$this->customer_id))->find();
        if(empty($menu)){
            $data['pid'] = 0;
        }else{
            $data['pid'] = $menu['id'];
        }
        $data['customer_id'] = $this->customer_id;
        $res = I("id") ? M('WxMenu')->save($data) : M('WxMenu')->add($data);
        $res === false ? $this->error('保存失败') : $this->success('保存成功');
    }

    /*
     *编辑菜单
     */
    public function editMenu(){
        $item = M('WxMenu')->where(array("id"=>(int)I("id")))->find();
        $this->success($item);
    }

    /**
     * 删除菜单
     */
    public function deleteMenu(){
        $res = M('WxMenu')->delete((int)I("id"));
        $res == false ? $this->error('删除失败') : $this->success('删除成功');
    }

    /**
     * 生成菜单
     */
    public function createMenu() {
        $first = M('WxMenu')->where(array("position"=>1,'customer_id'=>$this->customer_id))->order("id desc")->select();
        if(count($first)>1){
            for($i=0;$i<count($first)-1;$i++){
                $arr[] = array("type"=>$first[$i]['type'],"name"=>$first[$i]['title'],"key"=>$first[$i]['url']);
            }
            $menu[0] = array("name"=>$first[count($first)-1]['title'],"sub_button"=>$arr);
        }elseif($first){
            $menu[0] = array("type"=>$first[0]['type'],"name"=>$first[0]['title'],"key"=>$first[0]['url']);
        }

        $second = M('WxMenu')->where(array("position"=>2,'customer_id'=>$this->customer_id))->order("id desc")->select();
        if(count($second)>1){
            for($i=0;$i<count($second)-1;$i++){
                $arr2[] = array("type"=>$second[$i]['type'],"name"=>$second[$i]['title'],"key"=>$second[$i]['url']);
            }
            $menu[1] = array("name"=>$second[count($second)-1]['title'],"sub_button"=>$arr2);
        }elseif($second){
            $menu[1] = array("type"=>$second[0]['type'],"name"=>$second[0]['title'],"key"=>$second[0]['url']);
        }

        $third = M('WxMenu')->where(array("position"=>3,'customer_id'=>$this->customer_id))->order("id desc")->select();
        if(count($third)>1){
            for($i=0;$i<count($third)-1;$i++){
                $arr3[] = array("type"=>$third[$i]['type'],"name"=>$third[$i]['title'],"key"=>$third[$i]['url']);
            }
            $menu[2] = array("name"=>$third[count($third)-1]['title'],"sub_button"=>$arr3);
        }elseif($third){
            $menu[2] = array("type"=>$third[0]['type'],"name"=>$third[0]['title'],"key"=>$third[0]['url']);
        }

        $target      = array();
        $menuService = new \Overtrue\Wechat\Menu(C('APPID'), C('APPSECRET'));

        // 构建你的菜单
        foreach ($menu as $mn) {
            // 创建一个菜单项
            $item = new \Overtrue\Wechat\MenuItem($mn['name']);

            // 子菜单
            if (!empty($mn['sub_button'])) {
                $buttons = array();
                foreach ($mn['sub_button'] as $button) {
                    $buttons[] = new \Overtrue\Wechat\MenuItem($button['name'], $button['type'], $button['key']);
                }
                $item->buttons($buttons);
            }
            $target[] = $item;
        }

        try {
            $menuService->set($target);
            $this->success('发布成功');
        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
    }
    
}
